package com.sy.common.service;

import com.google.gson.Gson;
import com.sy.cloud.guess.model.Gokguessnum;
import com.sy.cloud.guess.model.Heroset;
import com.sy.cloud.guess.model.JOrder;
import com.sy.cloud.guess.model.Orderbuffer;
import com.sy.cloud.wechat.model.WechatUser;
import com.sy.cloud.wechat.model.Wxuser;
import com.sy.common.model.Juejinuser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by mac on 2017/12/6.
 */
@Transactional
@Service
@Slf4j
public class TestService implements ITestService{
    @Resource
    private IBaseService baseService;

    @Override
    public void addUser(Juejinuser juejinuser){
        baseService.update(juejinuser);
    }

    @Override
    public void addModel(WechatUser wechatUser) {
        baseService.update(wechatUser);
    }

    @Override
    public WechatUser getWechatInfo(String openid) {

        StringBuilder hql = new StringBuilder();
        hql.append( "From WechatUser where open_id = '" );
        hql.append( openid );
        hql.append("'");

        WechatUser wechatUser = baseService.queryObjectFromMaster(  hql.toString(), WechatUser.class );
        return wechatUser;
    }

    @Override
    public void updateUser(Juejinuser juejinuser) {
        baseService.update( juejinuser );
    }

    @Override
    public Double getMoney(String phone) {
        StringBuilder selectSql0 = new StringBuilder();
        selectSql0.append(" From Juejinuser u where phone = '");
        selectSql0.append( phone );
        selectSql0.append("'");
        List<Juejinuser> list = baseService.listQuery(selectSql0.toString(),Juejinuser.class );


        if( list.size() > 0 ){
            Juejinuser juejinuser = list.get(0);
            return juejinuser.getMoney();
        }
        return 0.0;

    }

    @Override
    public boolean dealdb(JOrder order) {
        try{
            baseService.create( order );

            // 往buffer里面更新记录
            List<Orderbuffer> orderbuffers = baseService.listQuery("From Orderbuffer", Orderbuffer.class);

            Orderbuffer orderbuffer = orderbuffers.get(0);
            switch ( order.getCapital() ){
                case 10:
                    orderbuffer.setT0(  orderbuffer.getT0() + order.getAmount() );
                    break;
                case 11:
                    orderbuffer.setT1(  orderbuffer.getT1() + order.getAmount() );
                    break;
                case 12:
                    orderbuffer.setT2(  orderbuffer.getT2() + order.getAmount() );
                    break;
                case 13:
                    orderbuffer.setT3(  orderbuffer.getT3() + order.getAmount() );
                    break;
                case 14:
                    orderbuffer.setT4(  orderbuffer.getT4() + order.getAmount() );
                    break;
                case 15:
                    orderbuffer.setT5(  orderbuffer.getT5() + order.getAmount() );
                    break;
            }

            baseService.update( orderbuffer );



            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 把计算出来的中奖结果存到表中
     * @param gok
     * @return
     */
    @Override
    public boolean updateGokguessnum(Gokguessnum gok) {
        // 添加中奖结果
        long l1 = System.currentTimeMillis()/1000;
        gok.setCreate_time( l1+"" );

        baseService.create( gok );
        // 更新订单缓存中的记录，清0
        List<Orderbuffer> orderbuffers = baseService.listQuery("From Orderbuffer", Orderbuffer.class );
        Orderbuffer orderbuffer = orderbuffers.get(0);

        // 更新订单缓存中的金额，清0
        orderbuffer.setT0(0);
        orderbuffer.setT1(0);
        orderbuffer.setT2(0);
        orderbuffer.setT3(0);
        orderbuffer.setT4(0);
        orderbuffer.setT5(0);
//        log.info(  );
        baseService.update( orderbuffer );

        // 更新订单表里的记录
        updateWinaOrLose( gok.getIssue(), gok );
        return true;
    }

    /**
     * 获得当前最大的issue
     * @return
     */
    @Override
    public String getMaxIssue() {
        String hql ="select max(issue) as adf from sp_gokguess_num";
        List<Object[]> objectslist = new ArrayList<>();
        Integer id = 1;
        try{
            objectslist = baseService.nativeQuery(hql);
            if( objectslist.size() > 0 ){
                Object object = objectslist.get(0);
                id = (int)object;
            }
        }catch (Exception e){

        }
        return id.toString();
    }

    /**
     * 更新订单列表中的所有未结算订单
     */
    @Override
    public void updateWinaOrLose( Integer issue, Gokguessnum gok) {
        long ter = System.currentTimeMillis();
        long ter1 = ter/1000;
        int i0 = (int)ter1;
        int i1 = (int)System.currentTimeMillis()/1000 - 100;
        i1 = i0-100;
        StringBuilder hql = new StringBuilder();
//        hql.append("From Order where issue is null and createtime > ");
//        hql.append("From JOrder where issue is null");
        hql.append("From JOrder where issue is null");
//        hql.append( i1 );
        log.info( hql.toString() );
        List<JOrder> orders = baseService.listQuery( hql.toString(), JOrder.class );




        for( int i = 0;i < orders.size(); i++ ){
            JOrder order = orders.get(i);
            order.setIs_win(0);
            // 判断精确场
            switch ( order.getCapital() ){
                case 10:
                    // 客户买单
                    if( gok.isSingle() ){
                        order.setIs_win(1);
                    }
                    break;
                case 11:
                    // 客户买双
                    if( gok.isIfdouble() ){
                        order.setIs_win(1);
                    }
                    break;
                case 12:
                    // 客户买大
                    if( gok.isBig() ) {
                        order.setIs_win(1);
                    }
                    break;
                case 13:
                    // 客户买小
                    if( gok.isLittle() ){
                        order.setIs_win(1);
                    }
                    break;
                case 14:
                    // 客户买顺子
                    if( gok.isShunzi() ){
                        order.setIs_win(1);
                    }
                    break;
                case 15:
                    // 客户买三条
                    if( gok.isSan() ){
                        order.setIs_win(1);
                    }
                    break;
            }
            // 如果这笔订单是英雄场
            if( order.getCapital() <= 10 ){
                if( order.getCapital() == gok.getHeroId() ){
                    // 中奖
                    order.setIs_win(1);
                }
            }

            if( order.getIs_win() == 1 ){
                // 客户赢了 ，加钱
                StringBuilder sb = new StringBuilder();
                sb.append("From Juejinuser where id = ");
                sb.append( order.getUserid() );
                Juejinuser juejinuser = baseService.queryObject( sb.toString(), Juejinuser.class );
                juejinuser.setMoney( juejinuser.getMoney()+  order.getGet_amount() );
                baseService.update( juejinuser );
                log.info( "订单"+order.getId()+"赢利"+ (order.getGet_amount()-order.getAmount()) );
            }


            order.setIssue( issue );
            baseService.update( order );


        }
    }


    /**
     * 英雄的列表
     * @return
     */
    @Override
    public List<Heroset> getList(){
        List<Heroset> herosetList = new ArrayList<Heroset>();

        StringBuilder selectSql0 = new StringBuilder();

        selectSql0.append(" From Heroset u where type = 1");
        herosetList = baseService.listQuery( selectSql0.toString(), Heroset.class );
        return herosetList;
    }

    /**
     * 王者荣耀在线人数的配置列表
     * @return
     */
    public List<Heroset> getList2(){
        List<Heroset> herosetList = new ArrayList<Heroset>();

        StringBuilder selectSql0 = new StringBuilder();
        selectSql0.append(" From Heroset u where type = 2");
        herosetList = baseService.listQuery( selectSql0.toString(), Heroset.class );
        return herosetList;
    }

    /**
     * 名字和id的对应
     * @return
     */
    @Override
    public List<Object[]> getList3(){
        List<Object[]> allList = new ArrayList<Object[]>();

        StringBuilder selectSql0 = new StringBuilder();
        selectSql0.append("select name From Heroset u where 1=1");
//        allList = dbService.getHeroList( selectSql0.toString() );
        allList = baseService.nativeQuery("select id,name,odds from sp_hero_set");
        return allList;
    }

    /**
     * 概率
     * @return
     */
    public Map<String,Double> probaSet(String type){
        Map<String,Double> mapresult = new HashMap<String,Double>();
        Heroset heroset = new Heroset();
        List l0 = new ArrayList();
        if( "hero".equals(type) ){
            l0  = getList();
            Iterator it = getList().iterator();
            while( it.hasNext() ){
                heroset = (Heroset)it.next();
                mapresult.put(heroset.getId().toString(), heroset.getPercent());
            }
        }else if( "digit".equals(type) ){
            l0  = getList2();
            Iterator it = getList().iterator();
            while( it.hasNext() ){
                Heroset digitse = (Heroset)it.next();
                mapresult.put(digitse.getId().toString(), digitse.getPercent());
            }
        }


        return mapresult;
    }

    @Override
    public String getAccesstoken(String appid) {
        StringBuilder hql = new StringBuilder();
        hql.append("From Wxuser where wxid = '");
        hql.append( appid );
        hql.append( "'" );
        List<Wxuser> wxusers = baseService.listQuery( hql.toString(), Wxuser.class );
        Wxuser wxuser = wxusers.get(0);
        return wxuser.getAccessToken();
    }


    /**
     * 赔率
     * @return
     */
    @Override
    public Map<Integer,Integer> oddsSet(String type){
        Map<Integer,Integer> mapresult = new HashMap<Integer,Integer>();
        List l0 = new ArrayList();
        if( "hero".equals(type) ){
            l0  = getList();
            Iterator it = l0.iterator();
            while( it.hasNext() ){
                Heroset heroset = (Heroset)it.next();
                mapresult.put( heroset.getId().intValue(), heroset.getOdds());
            }
        }else if( "digit".equals(type) ){
            l0  = getList2();
            Iterator it = l0.iterator();
            while( it.hasNext() ){
                Heroset heroset = (Heroset)it.next();
                mapresult.put( heroset.getId().intValue(), heroset.getOdds());
            }
        }

        return mapresult;
    }


}
